package com.flashphoner.app;

import com.flashphoner.sdk.sip.ISipMessageListener;
import gov.nist.javax.sip.header.CallID;
import gov.nist.javax.sip.message.SIPMessage;

import java.text.ParseException;

/**
 * Created by IntelliJ IDEA.
 * User: R2011
 * Date: Feb 2, 2011
 * Time: 10:05:07 PM
 * To change this template use File | Settings | File Templates.
 */
public class ChangeCallIdListener implements ISipMessageListener {

    private final String COLON_SUNSTITUTION = "AbCdEfGh";

    @Override
    public void processMessage(SIPMessage sipMessage) {
        CallID callIdHeader = (CallID) sipMessage.getHeader(CallID.NAME);
        String callId = callIdHeader.getCallId();

        if (callId.indexOf(":") != -1) {
            callId = callId.replaceAll(":", COLON_SUNSTITUTION);
        } else if (callId.indexOf(COLON_SUNSTITUTION) != -1) {
            callId = callId.replaceAll(COLON_SUNSTITUTION, ":");
        }

        try {
            callIdHeader.setCallId(callId);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
